package com.example.HelloSpace;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.HelloSpace.adapters.MissionInfoAdapter;
import com.example.HelloSpace.factory.MissionListViewModelFactory;
import com.example.HelloSpace.utils.MyApp;
import com.example.HelloSpace.utils.ToastShow;
import com.example.HelloSpace.viewmodels.MissionListViewModel;

import java.util.ArrayList;


public class LaunchTaskFragment extends Fragment {

    private MissionListViewModel model;
    private MissionInfoAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;


    public LaunchTaskFragment() {
        // Required empty public constructor
    }

    public static LaunchTaskFragment newInstance(String param1, String param2) {
        LaunchTaskFragment fragment = new LaunchTaskFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_launch_task, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final String URL = getResources().getString(R.string.SERVER_URL);
        // 初始化 ViewModel
        model = new ViewModelProvider(
                this,
                new MissionListViewModelFactory(((MainActivity) requireActivity()).getMissionRepository())
        ).get(MissionListViewModel.class);

        // 初始化 RecyclerView 和适配器
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        adapter = new MissionInfoAdapter(new ArrayList<>());
        adapter.setMissionListViewModel(model);
        recyclerView.setAdapter(adapter);

        // 设置 SwipeRefreshLayout 的监听器
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(() -> {
            model.refreshMissions(); // 重新加载数据
        });

        // 观察数据变化
        model.getAllMissions().observe(getViewLifecycleOwner(), missionInfoModels -> {
            // 更新适配器
            if (missionInfoModels != null) {
                adapter.updateMissionInfoList(missionInfoModels);

                // 仅在数据更新时显示 Toast
                if (swipeRefreshLayout.isRefreshing()) {
                    ToastShow.show(requireContext(), "数据已更新");
                }
            } else {
                Log.w("Refresh", "未能获取到有效的任务列表");
            }

            // 停止刷新状态
            swipeRefreshLayout.setRefreshing(false);
        });

        // 初次加载数据
        model.refreshMissions(); // 进行初次加载
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        model.getAllMissions().removeObservers(getViewLifecycleOwner());
        model.getSelectedMission().removeObservers(getViewLifecycleOwner());
    }

    @Override
    public void onResume() {
        super.onResume();
        MyApp.updateStatusBarColorAndIcons(false, requireActivity());
    }
}